package yingyong;

import gongju.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/insert")
public class insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html;character=utf8");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        int age = Integer.parseInt(req.getParameter("age"));
        String tel = req.getParameter("tel");
        String pwd = req.getParameter("pwd");
        int dname = Integer.parseInt(req.getParameter("dname"));
        String sql = "insert into Emp values (0,?,?,?,?,?,?)";
        int i = 0;
        try {
            i = DBUtil.update(sql, name, sex, age, tel, pwd, dname);
            resp.setCharacterEncoding("utf8");
            if (i>0){
                resp.getWriter().write("<script>alert('添加成功');location.href='/select';</script>");
            }else {
                resp.getWriter().write("<script>alert('添加失败');location.href='/select';</script>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
